using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
/*
 * 
 * 
 * 
 */
namespace Consolium
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GameModel.Model model;
        Button endTurnButton;
        Button attackButton;
        Button exitButton;
        Button newGameButton;
        SpriteFont font;
        string resourceLbl;
        string hpStatLbl;
        string atkStatLbl;
        string defStatLbl;
        string retalStatLbl;
        string trnStatLbl;
        Vector2 resourcePos;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        List<Texture2D> units = new List<Texture2D>();//holds 1 Texture2D object for each unit type
        List<Texture2D> terrain = new List<Texture2D>(); //holds 1 Texture2D object for each terrain type
        Texture2D border;
        int tileWidth = 40; //tile dimensions
        int tileHeight = 40;
        int[,] map = {
            {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,},
            {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,},
            {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 ,0 ,0 ,0 ,0 ,0 ,},
            {0, 0, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,},
            {0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,},
            {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,},
            {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0,0,0,0,0,0,},
            {0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,},
            };
        int[,] unitmap = {
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0, },
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,0,0,0,0,},
            {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,},
            };
        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.graphics.PreferredBackBufferHeight=800;
            this.graphics.PreferredBackBufferWidth = 1000;

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.IsMouseVisible = true;
            base.Initialize();
            model = new GameModel.Model("test.map", "test.scenario");
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            font = Content.Load<SpriteFont>("Times New Roman");
            resourcePos = new Vector2(50, 700);
            spriteBatch = new SpriteBatch(GraphicsDevice);
            border = Content.Load<Texture2D>("Border");
            // TODO: use this.Content to load your game content here
            //load terrain types into terrain List
            terrain.Add(Content.Load<Texture2D>("GrassTile40x40"));
            terrain.Add(Content.Load<Texture2D>("ForestTile40x40"));
            terrain.Add(Content.Load<Texture2D>("MountainTile40x40"));
            terrain.Add(Content.Load<Texture2D>("MineTile40x40"));
            //load unit types into unit List
            units.Add(Content.Load<Texture2D>("BlueSpearmanR30x30"));
            units.Add(Content.Load<Texture2D>("BlueArcherR30x30"));
            units.Add(Content.Load<Texture2D>("BlueHorsemanR40x40"));
            units.Add(Content.Load<Texture2D>("BlueGathererR30x30"));

            //create map as 2 dimensional array

            //create buttons
            Texture2D texture;
            texture = Content.Load<Texture2D>("button");
            endTurnButton = new Button(texture, font, spriteBatch);
            endTurnButton.Text="End Turn";
            attackButton = new Button(texture, font, spriteBatch);
            attackButton.Text = "Attack";
            exitButton = new Button(texture, font, spriteBatch);
            exitButton.Text = "Exit";
            newGameButton = new Button(texture, font, spriteBatch);
            newGameButton.Text = "New Game";


           
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
      
        protected override void Update(GameTime gameTime)
        { //deal with the controller - figure out what to draw
            // Allows the game to exit
            MouseState state = Mouse.GetState();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            // TODO: Add your update logic here
            resourceLbl = "Resources Available: ";
            hpStatLbl = "HP: ";
            atkStatLbl = "Attack: ";
            defStatLbl = "Defense: ";
            retalStatLbl = "Retaliation Chance: ";
            trnStatLbl = "Terrain: ";
            if (state.LeftButton == ButtonState.Pressed)
            {
                int yTile = state.X/40;
                int xTile = state.Y/40;
                if (!(xTile < 0) && !(yTile <0))
                {
                    try
                    {
                    //    map[xTile, yTile] = 2;
                        hpStatLbl = "X:" + state.X + " Y:" + state.Y;
                    }
                    catch (System.IndexOutOfRangeException e)
                    {
                    }
                }
            }

            //Buttons must be updated location Wise!!!!!
            endTurnButton.Location(600, 700);
            attackButton.Location(850, 700);
            endTurnButton.Update();
            attackButton.Update();
            exitButton.Location(600, 700);
            newGameButton.Location(850, 700);
            exitButton.Update();
            newGameButton.Update();
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        { //place graphics on the screen****************
            GraphicsDevice.Clear(new Color(139,69,019));

            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteSortMode.Deferred,BlendState.AlphaBlend);
            spriteBatch.Draw(border, new Rectangle(0, 640, 1000, 160), Color.White);
            //Draws the Map onto the board
            for (int y = 0; y < map.GetLength(0); y++)
            {
              for (int x = 0; x < map.GetLength(1); x++)
               {
                spriteBatch.Draw(terrain[map[y, x]],new Rectangle(x * tileWidth,y * tileHeight,tileWidth,tileHeight),Color.White);
               }
            }
            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.Deferred,BlendState.AlphaBlend);
            //Overlays the units onto the board
            for (int y = 0; y < map.GetLength(0); y++)
            {
                for (int x = 0; x < map.GetLength(1); x++)
                {
                    if(unitmap[y,x]!=0)
                    {
                    spriteBatch.Draw(units[(unitmap[y, x]-1)], new Rectangle(x * tileWidth,y * tileHeight,tileWidth,tileHeight), Color.White);
                    }
                }
            }
            spriteBatch.DrawString(font, resourceLbl, new Vector2(50,660), Color.Black);
            spriteBatch.DrawString(font, hpStatLbl, new Vector2(320, 660), Color.Black);
            spriteBatch.DrawString(font, atkStatLbl, new Vector2(320, 685), Color.Black);
            spriteBatch.DrawString(font, defStatLbl, new Vector2(320, 710), Color.Black);
            spriteBatch.DrawString(font, retalStatLbl, new Vector2(320, 735), Color.Black);
            spriteBatch.DrawString(font, trnStatLbl, new Vector2(320, 760), Color.Black);
            spriteBatch.End();

            endTurnButton.Draw();
            attackButton.Draw();
            exitButton.Draw();
            newGameButton.Draw();
            base.Draw(gameTime);
        }
    }
}
